<template>
  <div>
    <!-- 头部导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card class="box-card">
      <el-button @click="showAddCateDialog" type="primary">添加分类</el-button>

      <!-- 树状结构 -->
      <tree-table
        style="margin: 5px;"
        :data="cateList"
        :columns="columns"
        :show-index="true"
        :selection-type="false"
        :show-row-hover="false"
        :expand-type="false"
        index-text="#"
        border
      >
        <template slot="isok" slot-scope="scope">
          <i v-if="scope.row.cat_deleted==false" class="el-icon-success" style="color:lightgreen;"></i>
          <i v-else class="el-icon-error" style="color:red;"></i>
        </template>
        <template slot="order" slot-scope="scope">
          <el-tag size="mini" v-if="scope.row.cat_level==0" type="danger">一级</el-tag>
          <el-tag size="mini" v-else-if="scope.row.cat_level==1" type="success">二级</el-tag>
          <el-tag size="mini" v-else type="warning">三级</el-tag>
        </template>
        <template slot="opt" slot-scope="scope">
          
          <!-- 编辑按钮 -->
          <el-button size="mini" type="primary" icon="el-icon-edit">编辑</el-button>
          <!-- 删除按钮 -->
          <el-button
            size="mini"
            type="danger"
            icon="el-icon-delete"
            @click="delCateForm(scope.row)"
          >删除</el-button>
        </template>
      </tree-table>

      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryData.pagenum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="queryData.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>

    <!-- 添加分类的对话框 -->
    <el-dialog @close="resetCateForm" title="添加用户" :visible.sync="addCateDialogVisible" width="50%">
      <!-- 内容主体部分 -->
      <el-form 
      :model="cateForm" 
      :rules="addCateRules" 
      ref="cateForm" 
      label-width="100px">
        <el-form-item label="分类名称" prop="cat_name">
          <el-input v-model="cateForm.cat_name"></el-input>
        </el-form-item>
        <el-form-item label="父级分类">
          <!-- option数据源 -->
          <el-cascader
            v-model="selectOptionArr"
            :options="parentCateList"
            :props="cascaderProps"
            @change="cascaderHandleChange"
            clearable
          ></el-cascader>
        </el-form-item>
      </el-form>
      <!-- 底部区域 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="addCateDialogVisible = false">取 消</el-button>
        <el-button @click="addCateSub" type="primary">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      queryData: {
        type: 3,
        pagenum: 1,
        pagesize: 5
      },
      total: 0,
      cateList: [],
      total: 0,
      //   每个{}内是一列数据
      columns: [
        { label: "分类名称", prop: "cat_name" },
        { label: "是否有效", type: "template", template: "isok" },
        { label: "排序", type: "template", template: "order" },
        { label: "操作", type: "template", template: "opt" }
      ],
      addCateDialogVisible: false,
      cateForm: {
        cat_name: "",
        cat_pid: 0,
        cat_level: 0
      },
      addCateRules: {
        cat_name: [
          { required: true, message: "请输入分类名称", trigger: "blur" }
        ]
      },
      parentCateList: [],
      selectOptionArr: [],
      cascaderProps: {
        expandTrigger: "hover",
        label: "cat_name",
        value: "cat_id",
        checkStrictly: true
      }
    };
  },

  methods: {
    // 获取商品分类
    async getGoodsCate() {
      var res = await this.axios.get("/categories", {
        params: this.queryData
      });
      console.log(res);
      if (res.meta.status == 200) {
        this.cateList = res.data.result;
        this.total = res.data.total;
      }
      console.log(this.cateList);
    },

    // 每页多少条发生变化时触发
    handleSizeChange(val) {
      console.log(val);
      this.queryData.pagesize = val;
      this.getGoodsCate();
    },
    // 页码发生变化时触发
    handleCurrentChange(val) {
      console.log(val);
      this.queryData.pagenum = val;
      this.getGoodsCate();
    },

    // 展示添加分类对话框
    async showAddCateDialog() {
      this.addCateDialogVisible = true;
      var res = await this.axios.get("/categories", {
        params: { type: 2 }
      });
      if (res.meta.status == 200) {
        this.parentCateList = res.data;
      }
      console.log(this.parentCateList);
    },
    // 选择分类后触发
    cascaderHandleChange() {
      if (this.selectOptionArr.length > 0) {
        this.cateForm.cat_pid = this.selectOptionArr[
          this.selectOptionArr.length - 1
        ];
        this.cateForm.cat_level = this.selectOptionArr.length;
      } else {
        this.cateForm.cat_pid = 0;
        this.cateForm.level = 0;
      }
    },
    // 重置添加分类表单
    resetCateForm() {
      this.$refs.cateForm.resetFields();
      this.selectOptionArr = [];
      this.cateForm.cat_level = 0;
      this.cateForm.cat_pid = 0;
    },
    //提交添加分类
    addCateSub() {
      this.$refs.cateForm.validate(async flag => {
        if (!flag) return;
        var res = await this.axios.post("categories", this.cateForm);
        console.log(res);
        if (res.meta.status == 201) {
          this.$mg.success("添加成功");
          this.addCateDialogVisible = false;
          this.getGoodsCate();
        } else {
          this.$mg.error(res.meta.msg);
        }
      });
    },

    // 删除分类表单
    delCateForm(row) {
      console.log(row);
      
      this.$cfm("此操作将永久删除该分类, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(async () => {
          var res = await this.axios.delete(`categories/+${row.cat_id}`);
            console.log(res);
          if (res.meta.status == 200) {
            this.$mg.success("删除成功");
            this.getGoodsCate();
          } else {
            this.$mg.error(res.meta.msg);
          }
        })
        .catch(() => {
          this.$mg.info("已取消删除");
        });
    }
  },

  created() {
    this.getGoodsCate();
  }
};
</script>

<style lang='less' scoped>
</style>
